package com.health.trans.pojo;

import cn.hutool.core.util.ArrayUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.health.trans.common.util.GenericUtil;
import com.health.trans.enums.SqlCondition;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.function.BiConsumer;

/**
 * @author nqlz
 */
@Data
@Slf4j
@ApiModel(description = "字段信息")
@ApiIgnore
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class FieldInfo {

    @ApiModelProperty("字段名")
    @NotBlank(message = "字段名不能为空。")
    private String name;

    @ApiModelProperty("数据库字段名")
    private String dbName;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty("数据开始时间")
    private Date startTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty("数据结束时间")
    private Date endTime;

    @ApiModelProperty(value = "字段类型", hidden = true)
    private Class<?> type;

    @ApiModelProperty("字段值")
    private Object value;

    @ApiModelProperty("条件关系")
    private SqlCondition condition = SqlCondition.eq;

    public <Y> Class<Y> getType() {
        return GenericUtil.get(type);
    }

    public <Y> Y getValue() {
        return GenericUtil.get(value);
    }

    public boolean dateTimeAllNotNull() {
        return ArrayUtil.isAllNotNull(startTime, endTime);
    }

    public void getDatePredicate(BiConsumer<Date, Date> dateConsumer) {
        if (dateTimeAllNotNull()) {
            dateConsumer.accept(startTime, endTime);
        }
    }

}
